<?php

namespace App\Imports;

use App\model\customer\Customer;
use App\model\customer\CustomerArea;
use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerHangye;
use App\model\customer\CustomerHuanping;
use App\model\customer\CustomerPeidian;
use App\model\customer\CustomerSource;
use App\model\customer\CustomerWuye;
use App\model\customer\CustomerXiaofang;
use App\model\customer\CustomerXuqiu;
use App\model\customer\CustomerMobile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use function GuzzleHttp\Psr7\uri_for;

class CustomerImport implements ToCollection
{

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        foreach ($collection as $k => $row) {

            if ($k != 0) {
                $customer = new Customer();
                $customer->name = $row[0];
                $customer->telephone = $row[1];
                $customer->address = $row[2];
                if (isset($row[3])) {
                    $customer->hangye_format = $row[3];
                    $hangye = CustomerHangye::whereName($row[3])->first();
                    if ($hangye) {
                        $customer->hangye_id = $hangye->hangye_id;
                    }
                }
                if (isset($row[4])) {
                    $customer->area_format = $row[4];
                    $area = CustomerArea::whereName($row[4])->first();
                    if ($area) {
                        $customer->area_id = $area->area_id;
                    }
                }
                if (isset($row[5])) {
                    $peidain = CustomerPeidian::whereName($row[5])->first();
                    $customer->peidian_format = $row[5];
                    if ($peidain) {
                        $customer->peidian_id = $peidain->peidian_id;
                    }
                }

                if (isset($row[6])) {
                    $customer->xuqiu_format = $row[6];
                    $xuqiu = CustomerXuqiu::whereName($row[6])->first();
                    if ($xuqiu) {
                        $customer->xuqiu_id = $xuqiu->xuqiu_id;
                    }
                }

                if (isset($row[7])) {
                    $customer->source_format = $row[7];
                    $soucre = CustomerSource::whereName($row[7])->first();
                    if ($soucre) {
                        $customer->source_id = $soucre->source_id;
                    }
                }
                if (isset($row[8])) {
                    $customer->xiaofang_format = $row[8];
                    $xiaofang = CustomerXiaofang::whereName($row[8])->first();
                    if ($xiaofang) {
                        $customer->xiaofang_id = $xiaofang->xiaofang_id;
                    }
                }

                if (isset($row[9])) {
                    $customer->huanping_format = $row[9];
                    $huanping = CustomerHuanping::  whereName($row[9])->first();
                    if ($huanping) {
                        $customer->huanping_id = $huanping->huanping_id;
                    }
                }
                if (isset($row[10])) {
                    $customer->desc = $row[10];
                }
                if (isset($row[11])) {
                    $customer->create_user_name = $row[11];
                }

                if (isset($row[12])) {
                    $bigPart = CustomerBigDepart::whereName($row[12])->first();
                    if ($bigPart) {
                        $customer->big_part_id = $bigPart->id;
                    }
                }
                if (isset($row[13])) {
                    $customer->wuye_format = $row[13];
                    $wuye = CustomerWuye::whereName($row[13])->first();
                    if ($wuye) {
                        $customer->wuye_id = $wuye->wuye_id;
                    }
                }
                $customer->record_time = Carbon::now();
                $customer->save();
                $customer->code = Customer::getCode($customer);
                $customer->save();
                $customerMobile= new CustomerMobile();
                $customerMobile->mobile=$customer->telephone;
                $customerMobile->customer_id=$customer->customer_id;
                $customerMobile->save();
            }
        }
    }
}
